package com.serie402.web.jsf.mb;

import java.util.List;

import com.kiss.fw.jsf.mb.AbstractManagedBean;
import com.kiss.fw.utils.Util;
import com.serie402.common.bo.PhotoAlbum;
import com.serie402.common.dto.page.GalleryPageDTO;
import com.serie402.common.utils.Constants;

public class GalleryManagedBean extends AbstractManagedBean {
	public String getWindowTitle() {
		return getWindowTitle(Constants.Mappings.GALLERY_DTO);
	}
	
	public String getPageTitle() {
		return getPageTitle(Constants.Mappings.GALLERY_DTO);
	}
	
	public GalleryPageDTO getDto() {
		return (GalleryPageDTO)getRequestScope().get(Constants.Mappings.GALLERY_DTO);
	}
	
	/**
	 * This method was created because using <c:foreach> was not giving
	 * the flexibility or the results required for the displayed desired.
	 * 
	 * @return String
	 */
	public String getPhotoAlbumsHtml() {
		List<PhotoAlbum> photoAlbums = getDto().getPhotoAlbums();
		StringBuffer sb = new StringBuffer();
		
		for(int i=0; i < photoAlbums.size(); i++) {
			// start tag
			sb.append((i % 2 == 0) ? "<tr>" : "");
			
			// append album column
			appendPhotoAlbumColumn(photoAlbums.get(i), sb);
			
			// closing tag -- only append if this is the second 
			// column or if this is the last album on the list
			sb.append(((i % 2 == 1) || i + 1 == photoAlbums.size()) ? "</tr>" : "");
		}
		
		return sb.toString();
	}
	
	/**
	 * This method will generate the code for the album column on the photo gallery page
	 * 
	 * @param PhotoAlbum _album
	 * @param StringBuffer _sb
	 */
	private void appendPhotoAlbumColumn(final PhotoAlbum _album, StringBuffer _sb) {
		_sb.append("<td class=\"GalleryListingColumn\"><table class=\"GalleryListingColumnTable\"><tr><td class=\"GalleryListingThumbnail\" style=\"background-image:url('");
		_sb.append(_album.getThumbnail().getUrl());
		_sb.append("');\" onclick=\"goToPhotoAlbum('");
		_sb.append(_album.getAlbumId());
		_sb.append("')\"></td><td class=\"GalleryListingDescription\"><span class=\"BlockDisplay\" style=\"padding-bottom:4px;\">");
		_sb.append(Util.getDisplayDate(_album.getAlbumDate(), Constants.Instances.DR_LOCALE));
		_sb.append("</span><span class=\"BlockDisplay\">");
		_sb.append(_album.getDescription());
		_sb.append("</span></td></tr></table></td>");
	}
}